
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Date: 3/8/12
 */
public class TimeTest {

    /**
      ========china========
     select trunc(gmt_create+ (16/24)),count(*) from
     (
     select r.* from product_wrong_cat_report r, vaccount v
     where r.company_id = v.id and v.SERVICE_TYPE in ('cfm','cgs','cnfm','gs','hkgs','twgs')
     ) group by trunc(gmt_create+ (16/24)) order by trunc(gmt_create+ (16/24)) desc

      ========us========
     select trunc(gmt_create),count(*) from
     (
     select r.* from product_wrong_cat_report r, vaccount v
     where r.company_id = v.id and v.SERVICE_TYPE in ('ifm','ofm','tp')
     ) group by trunc(gmt_create) order by trunc(gmt_create) desc





     */
    private static String _data1 = "\n" +
            "1\t2012-03-08 00:00:00.0\t252\n" +
            "2\t2012-03-07 00:00:00.0\t313\n" +
            "3\t2012-03-06 00:00:00.0\t163\n" +
            "4\t2012-03-05 00:00:00.0\t302\n" +
            "5\t2012-03-04 00:00:00.0\t84\n" +
            "6\t2012-03-03 00:00:00.0\t119\n" +
            "7\t2012-03-02 00:00:00.0\t158\n" +
            "8\t2012-03-01 00:00:00.0\t201\n" +
            "9\t2012-02-29 00:00:00.0\t140\n" +
            "10\t2012-02-28 00:00:00.0\t163\n" +
            "11\t2012-02-27 00:00:00.0\t101\n" +
            "12\t2012-02-26 00:00:00.0\t28\n" +
            "13\t2012-02-25 00:00:00.0\t34\n" +
            "14\t2012-02-24 00:00:00.0\t148\n" +
            "15\t2012-02-23 00:00:00.0\t106\n" +
            "16\t2012-02-22 00:00:00.0\t130\n" +
            "17\t2012-02-21 00:00:00.0\t164\n" +
            "18\t2012-02-20 00:00:00.0\t115\n" +
            "19\t2012-02-19 00:00:00.0\t26\n" +
            "20\t2012-02-18 00:00:00.0\t151\n" +
            "21\t2012-02-17 00:00:00.0\t114\n" +
            "22\t2012-02-16 00:00:00.0\t49\n" +
            "23\t2012-02-15 00:00:00.0\t81\n" +
            "24\t2012-02-14 00:00:00.0\t97\n" +
            "25\t2012-02-13 00:00:00.0\t125\n" +
            "26\t2012-02-12 00:00:00.0\t13\n" +
            "27\t2012-02-11 00:00:00.0\t52\n" +
            "28\t2012-02-10 00:00:00.0\t91\n" +
            "29\t2012-02-09 00:00:00.0\t82\n" +
            "30\t2012-02-08 00:00:00.0\t95\n" +
            "31\t2012-02-07 00:00:00.0\t54\n" +
            "32\t2012-02-06 00:00:00.0\t57\n" +
            "33\t2012-02-05 00:00:00.0\t7\n" +
            "34\t2012-02-04 00:00:00.0\t55\n" +
            "35\t2012-02-03 00:00:00.0\t170\n" +
            "36\t2012-02-02 00:00:00.0\t320\n" +
            "37\t2012-02-01 00:00:00.0\t108\n" +
            "38\t2012-01-31 00:00:00.0\t28";
    
    private static String _data2 = "\n" +
            "1\t2012-03-07 00:00:00.0\t6\n" +
            "2\t2012-03-06 00:00:00.0\t2\n" +
            "3\t2012-03-05 00:00:00.0\t6\n" +
            "4\t2012-03-04 00:00:00.0\t1\n" +
            "5\t2012-03-03 00:00:00.0\t8\n" +
            "6\t2012-03-02 00:00:00.0\t3\n" +
            "7\t2012-03-01 00:00:00.0\t15\n" +
            "8\t2012-02-27 00:00:00.0\t3\n" +
            "9\t2012-02-24 00:00:00.0\t1\n" +
            "10\t2012-02-23 00:00:00.0\t3\n" +
            "11\t2012-02-22 00:00:00.0\t1\n" +
            "12\t2012-02-21 00:00:00.0\t2\n" +
            "13\t2012-02-20 00:00:00.0\t2\n" +
            "14\t2012-02-17 00:00:00.0\t3\n" +
            "15\t2012-02-16 00:00:00.0\t1\n" +
            "16\t2012-02-15 00:00:00.0\t2\n" +
            "17\t2012-02-14 00:00:00.0\t2\n" +
            "18\t2012-02-13 00:00:00.0\t9\n" +
            "19\t2012-02-10 00:00:00.0\t10\n" +
            "20\t2012-02-09 00:00:00.0\t34\n" +
            "21\t2012-02-08 00:00:00.0\t9\n" +
            "22\t2012-02-07 00:00:00.0\t14\n" +
            "23\t2012-02-06 00:00:00.0\t6\n" +
            "24\t2012-02-05 00:00:00.0\t8\n" +
            "25\t2012-02-04 00:00:00.0\t2\n" +
            "26\t2012-02-03 00:00:00.0\t16\n" +
            "27\t2012-02-02 00:00:00.0\t17\n" +
            "28\t2012-02-01 00:00:00.0\t3\n" +
            "29\t2012-01-31 00:00:00.0\t4";

    public static void main(String[] args) throws ParseException {
        String theData = _data1;
        Integer[][] output = new Integer[100][];
        int index = 0;  // less then fommer 100
        int lastWeekOfYear = -1;
        String[] lines = theData.split("\n");
        for (String aLine : lines) {
            if (aLine != null && aLine.contains("\t")) {
                String[] aLineArray = aLine.split("\t");
                String dateStr = aLineArray[1];
                String count = aLineArray[2];
                Date date = getDate(dateStr);
                Calendar instance = Calendar.getInstance();
                instance.setTime(date);
                instance.setFirstDayOfWeek(Calendar.MONDAY);
                int dayOfWeek = instance.get(Calendar.DAY_OF_WEEK) - 1;
                int theWeekOfYear = instance.get(Calendar.WEEK_OF_YEAR);
                if (lastWeekOfYear == -1) {
                    output[++index] = new Integer[7];
                }
                if (lastWeekOfYear != theWeekOfYear) {
                    int weekDelta = lastWeekOfYear - theWeekOfYear;
                    for (int i=0;i<weekDelta;i++) {
                        output[++index] = new Integer[7];
                    }
                    lastWeekOfYear = theWeekOfYear;
                }

                if (dayOfWeek == 0) {
                    dayOfWeek = 7;
                }
                output[index][dayOfWeek - 1] = Integer.parseInt(count);
            }
        }
        for (Integer[] anOutLine : output) {
            if (anOutLine != null) {
                StringBuilder _sb = new StringBuilder();
                for (Integer aNum : anOutLine) {
                    if (aNum == null) {
                        aNum = -1;
                    }
                    _sb.append("[").append(aNum).append("] ");
                }
                System.out.println(_sb.toString());
            }
        }
    }

    private static Date getDate(String dateStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return simpleDateFormat.parse(dateStr);
    }
}
